package cn.wolfcode.mq.listener;

import cn.wolfcode.domain.OperateIntergralVo;
import cn.wolfcode.mq.MQConstant;
import cn.wolfcode.service.IUsableIntegralService;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

//4.上游服务执行成功，则下游服务执行积分退款操作
@Slf4j
@Component
@RocketMQMessageListener(
        consumerGroup = MQConstant.INTERGRAL_REFUND_TX_CONSUMER_GROUP,
        topic = MQConstant.INTERGRAL_REFUND_TX_TOP
)
public class IntergralRefundMessageListener implements RocketMQListener<OperateIntergralVo> {

    @Autowired
    private IUsableIntegralService usableIntegralService;

    @Override
    public void onMessage(OperateIntergralVo intergralVo) {
        log.info("[积分服务] 收到积分退款的消息,准备进行积分退款操作 {}", JSON.toJSONString(intergralVo));
        //积分退款操作
        try {
            usableIntegralService.refund(intergralVo);
            log.info("[积分服务] 积分退款成功");
        } catch (Exception e) {
            log.warn("[积分服务] 积分退款异常",e);
        }
    }
}
